<?php

namespace App\Http\Controllers\Api;

use App\Service\ArticleCommentService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleCommentController extends Controller
{
    /**
     * @api {post} api/article/comment/create  添加文章评论
     * @apiVersion 1.0.0
     * @apiName createArticleComment
     * @apiGroup Article
     *
     * @apiParam   {String} user_id 评论人id.
     * @apiParam   {String} article_id  文章id
     * @apiParam   {String} content  评论内容
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function createArticleComment(Request $request)
    {
        $input = $request->all();

        $rules = array(
            "user_id" => "required",
            "article_id" => "required",
            "content" => "required",
        );
        $messages = array(
            "user_id.required" => "用户为空",
            "article_id.required" => "文章id为空",
            "content.required" => "内容为空",);
        $result = $this->validate($input, $rules, $messages);
        if ($result["code"] != 0) {
            return $this->fail(self::VALIDATE_ERROR, $result["errors"]);
        }

        $res = ArticleCommentService::createArticleComment($input);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '添加失败');
        }
    }

    /**
     * @api {post} api/article/comment/{id}/delete  删除文章评论
     * @apiVersion 1.0.0
     * @apiName deleteArticleComment
     * @apiParam   {String} user_id 操作人.
     * @apiGroup Article
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function deleteArticleComment(Request $request, $id)
    {
        if (empty($request->input('user_id'))) {
            return $this->fail(self::VALIDATE_ERROR, '操作人id为空');
        }
        $user_id = $request->input('user_id');
        $res = ArticleCommentService::deleteArticleComment($id, $user_id);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '删除失败');
        }
    }

    /**
     * @api {post} api/article/comment/{id}/update  修改Article分类
     * @apiVersion 1.0.0
     * @apiName updateArticleComment
     * @apiGroup Article
     *
     * @apiParam   {String} [name] 发布文章.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function updateArticleComment(Request $request, $id)
    {
        $input = $request->all();

        $updateInfo = array();
        if (!empty($input['content'])) {
            $updateInfo['content'] = $input['content'];
        }

        $res = ArticleCommentService::updateArticleComment($id, $updateInfo);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '修改失败');
        }
    }

    /**
     * @api {get} api/article/comment/list  获取Article评论列表
     * @apiVersion 1.0.0
     * @apiName getArticleCommentList
     * @apiGroup Article
     * @apiParam   {String} article_id 文章id.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     * {}
     */
    public function getArticleCommentList(Request $request)
    {
        $input = $request->all();


        $condition = array();

        if (isset($input['article_id'])) {
            $condition['article_id'] = $input['article_id'];
        } else {
            return $this->fail(self::VALIDATE_ERROR, "article_id为空");
        }

        $res = ArticleCommentService::getArticleCommentList($input['article_id']);
        if ($res) {
            return $this->success($res);
        } else {
            return $this->fail(self::ERROR_CODE, '获取失败');
        }
    }

}
